package com.afei.oid;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class ToastMy {
	private static final int DEFAULT_SHOW_TIME = 5000;
	private static final int DEFAULT_SWITHC_TIME = 4000;
	private static final int REMOVE_MSG = 101;
	public static TextView tv;
	public static WindowManager mWindowManager;
	private static Context m_context;
	private static WindowManager.LayoutParams m_params = new WindowManager.LayoutParams();
	private int mTime;
	private int mShowLength;
	private int mShowPrev;
	private int mShowNarrationIndex;
	private String mRemainString;
	public ToastMy(Context context)
	{
		m_context = context;
	}
	public void showMessage(final String msg) {
		showMessage(msg, Toast.LENGTH_SHORT);
	}
	public void showMessage(final int msg) {
		showMessage( m_context.getString(msg), Toast.LENGTH_SHORT);
	}
	public void setGravity(int gravity,int x,int y)
	{
		m_params.gravity = gravity;
		m_params.y = y;
		m_params.x = x;		
	}
	public  void showMessage(String msg,int timems)
	{
		String show;
		int showcount = (timems + 3000)/DEFAULT_SHOW_TIME;
		if(showcount == 0)
			showcount++;
		if(timems < DEFAULT_SHOW_TIME)
		{
			mTime = timems;
		}
		else
		{
			mTime = DEFAULT_SHOW_TIME;
		}
		mShowNarrationIndex = 0;
		mShowLength = msg.length()/showcount;
		if(mShowLength > msg.length())
			mShowLength = msg.length();
		mRemainString = msg;
		show = GetNextShowNarration();//msg.substring(0,mShowLength);
		if(timems > DEFAULT_SHOW_TIME)
			show += "...";
		mShowPrev = mShowLength/3;
		mShowNarrationIndex = mShowLength;
		if(show == null)
			return;
		mWindowManager=(WindowManager) m_context.getSystemService(Context.WINDOW_SERVICE);
		if(tv==null)
		{
			tv=new ScrollText(m_context);
			tv.setTextSize(22);
			tv.setTextColor(0xffffffff);//(R.color.narr_text_color);
			tv.setBackgroundColor(0xa0000000);//(R.color.narr_back_color);
//			tv.setMaxLines(2);
//			tv.setSingleLine();
//			tv.setGravity(0x11);
//			tv.setFocusable(true);
//			tv.setFocusableInTouchMode(true);
//			tv.setMarqueeRepeatLimit(2);
//			tv.setHorizontallyScrolling(true);
//			tv.setEllipsize(TruncateAt.MARQUEE);
			tv.setPadding(0, 0, 0, 0);
		}
		tv.setText(show);
		if(tv.getParent()==null)
		{
			m_params.alpha = 1.0f;
			m_params.height = WindowManager.LayoutParams.WRAP_CONTENT;
			m_params.width = WindowManager.LayoutParams.WRAP_CONTENT;
			m_params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
			| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
			| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
			| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
			m_params.format = PixelFormat.TRANSLUCENT;
			m_params.windowAnimations = 0;
			mWindowManager.addView(tv, m_params);
			handler.sendEmptyMessageDelayed(REMOVE_MSG, timems);
		}
		else
		{
			mWindowManager.updateViewLayout(tv, m_params);
			handler.removeMessages(REMOVE_MSG);
			handler.sendEmptyMessageDelayed(REMOVE_MSG, timems);
		}
	}

	private String GetNextShowNarration()
	{
		int newendindex = mShowNarrationIndex + mShowLength;
		int dotindex = (mRemainString.indexOf(",", newendindex) == -1)?mRemainString.indexOf(".", newendindex):mRemainString.indexOf(",", newendindex);
		if(dotindex != -1 && dotindex > mShowNarrationIndex + mShowLength)
		{
			if(dotindex < newendindex)
				newendindex = dotindex;
		}
		else if(dotindex == -1)
		{
			int spaceindex = mRemainString.indexOf(" ", newendindex);
			if(spaceindex != -1 && spaceindex > mShowNarrationIndex + mShowLength)
				newendindex = spaceindex;
		}
		if(newendindex > mRemainString.length())
			newendindex = mRemainString.length();
		String show = mRemainString.substring(mShowNarrationIndex > 0?(mShowNarrationIndex - mShowPrev):mShowNarrationIndex,newendindex);
		mShowNarrationIndex += mShowLength;
		mShowNarrationIndex += newendindex - (mShowNarrationIndex + mShowLength);
		return show;
	}
	public Handler handler =new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what == REMOVE_MSG)
			{
				if(mRemainString.equals(""))
				{
					if(tv!=null && tv.getParent()!=null)
					{
						mWindowManager.removeView(tv);
					}
				}
				else if(mShowNarrationIndex < mRemainString.length())
				{
					if(mRemainString.length() - mShowNarrationIndex < 2 * mShowLength)
						mShowLength = mRemainString.length() - mShowNarrationIndex;
					String show = GetNextShowNarration();//mRemainString.substring(mShowNarrationIndex - mShowPrev,mShowNarrationIndex + mShowLength);
					mShowNarrationIndex += mShowLength;
					if(show != null && show.length() > 1)
						tv.setText(show);
					if(mRemainString.length() > mShowNarrationIndex)
						handler.sendEmptyMessageDelayed(REMOVE_MSG, DEFAULT_SHOW_TIME);
				}
			}
		}
	};

	public void cancelCurrentToast()
	{
		if(tv!=null && tv.getParent()!=null)
		{
			handler.removeMessages(REMOVE_MSG);
			mWindowManager.removeView(tv);
			mRemainString = "";
			mShowPrev = 0;
			mShowNarrationIndex = 0;
			mShowLength = 0;
		}
	}
}
